package lookas.repositories;

import java.util.ArrayList;
import java.util.Collection;

import lookas.model.Customer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

@Repository("customerMockRepository")
public class CustomerRepositoryMockImpl implements CustomerRepository {

    static final Log            log       = LogFactory.getLog(CustomerRepositoryMockImpl.class);

    private ArrayList<Customer> customers = new ArrayList<Customer>();

    public CustomerRepositoryMockImpl() {
        for (int i = 0; i < 100; i++) {
            String s = "";
            if (i < 10) {
                s = "0";
            }
            s += i;
            Customer c = new Customer("Jan", "Nowak" + s, "123456123" + s);
            customers.add(c);
        }
    }

    @Override
    public Collection<Customer> findAllCustomers() {
        return customers;
    }

    @Override
    public Customer findCustomerByPesel(String pesel) {
        for (Customer c : customers) {
            if (c.getPesel().equalsIgnoreCase(pesel)) {
                return c;
            }
        }
        return null;
    }

    @Override
    public Collection<Customer> findLimitedCustomers(int offset, int limit) {
        return new ArrayList<Customer>(customers).subList(offset, Math.min(offset + limit, customers.size()));
    }

    @Override
    public void save(Customer c) {
        int indexOf = customers.indexOf(c);
        if (indexOf < 0) {
            customers.add(c);
        } else {
            customers.set(indexOf, c);
        }
    }

    @Override
    public long size() {
        return customers.size();
    }

    @Override
    public void delete(Customer c) {
        customers.remove(c);
    }
}
